package com.liyuan.yiqixing;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import org.springframework.context.annotation.Bean;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@Component
public class Utils {
    @Bean
    SimpleDateFormat simpleDateFormat(){
        return new SimpleDateFormat("yyyy-MM-dd hh:mm");
    }

    public String retrieveUUID(){
        return UUID.randomUUID().toString().replace("-","");
    }
    public String retrieveVRCode(){
        String time = String.valueOf(System.currentTimeMillis());
        int len = time.length();
        StringBuilder s=new StringBuilder(time);
        int i_1 = (int)(Math.random()*len);
        int i_2 = (int)(Math.random()*len);
        int i_3 = (int)(Math.random()*len);
        int i_4 = (int)(Math.random()*len);
        StringBuilder ans = new StringBuilder();
        ans.append(s.charAt(i_1));
        ans.append(s.charAt(i_2));
        ans.append(s.charAt(i_3));
        ans.append(s.charAt(i_4));
        return String.valueOf(ans);
    }

    public String retrieveToken(UserDetails userDetails) {
        Date start = new Date();
        long curentTime = System.currentTimeMillis() + 1000* 60 *60 * 10;
        Date end = new Date(curentTime);
        String token = JWT.create().withAudience(userDetails.getUsername())
                .withIssuedAt(start)
                .withExpiresAt(end)
                .sign(Algorithm.HMAC256(userDetails.getPassword()));
        return token;
    }
    public enum EnumUtils{
        REPLY_TO_DISCUSSION(1),
        REPLY_TO_COMMENT(2);

        private Integer type;

        public Integer getType() {
            return type;
        }

        public void setType(Integer type) {
            this.type = type;
        }

        EnumUtils(Integer type) {
            this.type = type;
        }

        EnumUtils() {
        }
    }
}
